<template>
  <view>
    <view class="search-box">
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <!-- 搜索建议 -->
    <view class="sugg-list" v-if="kw.length!==0">
      <view class="sugg-item" v-for="item in searchResults" :key="item.goods_id" @click="gotoDetail(item)">
        <view class="goods-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <!-- 标题 -->
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clean"></uni-icons>
      </view>
      <!-- 列表 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in historyList" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        kw: '', // 输入内容
        searchResults: [], // 搜索建议
        historyList: [] // 搜索历史
      };
    },
    onLoad() {
      // 获取搜索历史并转换数组
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods: {
      // 输入
      input(e) {
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e
          this.getSearchList()
        }, 500)
      },
      // 获取搜索建议
      async getSearchList() {
        if (this.kw.trim() === '') return this.searchResults = []
        const {
          data
        } = await uni.$http.get('/api/public/v1/goods/qsearch', {
          query: this.kw
        })
        // 请求失败
        if (data.meta.status !== 200) return uni.$showMsg()
        // 成功
        this.searchResults = data.message
        this.saveSearchHistory()
      },
      // 点击搜索建议
      gotoDetail(item) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      },
      // 储存搜索记录 
      saveSearchHistory() {
        // 用set进行去重
        this.historyList = Array.from(new Set([this.kw, ...this.historyList]))
        // 转成字符串持久化存储
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      // 清空搜索历史
      clean() {
        this.historyList = [],
          uni.setStorageSync('kw', '[]')
      },
      // 点击搜索记录
      gotoGoodsList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }

    }
  }
</script>

<style lang="scss">
  // 搜索框
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  // 搜索建议
  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;

      .goods-name {
        white-space: nowrap; // 单行显示
        overflow: hidden;
        text-overflow: ellipsis; // 溢出用...表示
      }
    }
  }

  // 搜索历史
  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      height: 40px;
      align-items: center;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      .uni-tag {
        display: block;
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
</style>
